package com.evil.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evil.account.api.RemoteUserService;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.file.dto.download.history.AddFileDownloadHistoryReqDTO;
import com.evil.file.api.RemoteFileDownloadHistoryService;
import com.evil.file.mapper.FileDownloadHistoryMapper;
import com.evil.file.pojo.entity.FileDownloadHistory;
import com.evil.file.service.FileDownloadHistoryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件下载历史表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class FileDownloadHistoryServiceImpl extends ServiceImpl<FileDownloadHistoryMapper, FileDownloadHistory> implements FileDownloadHistoryService, RemoteFileDownloadHistoryService {

    private final LoginUtil loginUtil;

    private final RemoteUserService remoteUserService;

    private final FileLogServiceImpl fileLogService;

    /**
     * 新增文件下载记录
     *
     * @param addFileDownloadHistory addFileDownloadHistory
     */
    @Override
    public void add(AddFileDownloadHistoryReqDTO addFileDownloadHistory) {
        if (null == addFileDownloadHistory.getUserId()) {
            UserBaseRespDTO loginUserBase = loginUtil.getLoginUserBase();
            addFileDownloadHistory.setUserId(loginUserBase.getUserId());
            addFileDownloadHistory.setNickName(loginUserBase.getNickName());
        } else {
            remoteUserService.findBaseById(addFileDownloadHistory.getUserId());
        }

        addFileDownloadHistory.setEnterpriseId(loginUtil.getParamEnterpriseId(addFileDownloadHistory.getEnterpriseId()));
        FileDownloadHistory fileDownloadHistory = BeanUtil.copyProperties(addFileDownloadHistory, FileDownloadHistory.class);
        fileLogService.saveAndCheck(this.baseMapper, FileDownloadHistory::getDownloadHistoryId, fileDownloadHistory);
    }
}
